#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
db_bak_lifecycle_day=3
db_bak_target_dir="/xtrabackup"
db_bak_reserved_in_target_dir=("xtra1stbak" "2021-07-08_19-27-53")
script_exec_log="/mysql_script/clear.log"
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

##------------------------------------------------------------------------
func_month_str_to_int()
{
    m=0
    if [[ $1 == "Jan" ]];   then m=1
    elif [[ $1 == "Feb" ]]; then m=2
    elif [[ $1 == "Mar" ]]; then m=3
    elif [[ $1 == "Apr" ]]; then m=4
    elif [[ $1 == "May" ]]; then m=5
    elif [[ $1 == "Jun" ]]; then m=6
    elif [[ $1 == "Jul" ]]; then m=7
    elif [[ $1 == "Aug" ]]; then m=8
    elif [[ $1 == "Sep" ]]; then m=9
    elif [[ $1 == "Oct" ]]; then m=10
    elif [[ $1 == "Nov" ]]; then m=11
    elif [[ $1 == "Dec" ]]; then m=12
    fi
    return $m
}
##-------------------------------------------------------------------------


db_bak=("")
for bak in `ls -l $db_bak_target_dir | tail -n +2 | awk '{print $9}'`
do
    db_bak[${#db_bak[@]}]=$bak
done
#echo ${db_bak[*]}

db_bak_time_day=("")
for day in `ls -l $db_bak_target_dir | tail -n +2 | awk '{print $7}'`
do
    db_bak_time_day[${#db_bak_time_day[@]}]=$day
done
#echo ${db_bak_time_day[*]}

#"Jan" "Mar" "May" "Jul" "Aug" "Oct" "Dec"  31
#"Apr" "Jun" "Sep" "Nov" 30
#"Feb" year%4 -eq 0  29   year%4 -ne 0 28
db_bak_time_month=("")
for month in `ls -l $db_bak_target_dir | tail -n +2 | awk '{print $6}'`
do
    #m=0
    #if [[ $month == "Jan" ]];   then m=1
    #elif [[ $month == "Feb" ]]; then m=2
    #elif [[ $month == "Mar" ]]; then m=3
    #elif [[ $month == "Apr" ]]; then m=4
    #elif [[ $month == "May" ]]; then m=5
    #elif [[ $month == "Jun" ]]; then m=6
    #elif [[ $month == "Jul" ]]; then m=7
    #elif [[ $month == "Aug" ]]; then m=8
    #elif [[ $month == "Sep" ]]; then m=9
    #elif [[ $month == "Oct" ]]; then m=10
    #elif [[ $month == "Nov" ]]; then m=11
    #elif [[ $month == "Dec" ]]; then m=12
    #fi
    func_month_str_to_int $month                                                                             
    db_bak_time_month[${#db_bak_time_month[@]}]=$?
done
#echo ${db_bak_time_month[*]}

##---------------------------------------------------------------------------
func_item_in_array()
{
    array=$1; target=$2; ret=0
    for item in ${array[*]}; do
        if [[ $item == $target ]] ; then
            ret=1
            break
        fi
    done
    return $ret
}

##----------------------------------------------------------------------------
calc_last_date_rst=" "
calc_last_month=0
calc_last_day=0
func_calc_last_date()
{
    duration=$1
    if [[ duration -gt 30 ]]; then
        echo "Couldn't greater than 30 days!"
        calc_last_date_rst="na"
    else
        year=`date +"%Y"`; i_year=$((10#${year}))
        month=`date +"%m"`; i_month=$((10#${month}))
        day=`date +"%d"`; i_day=$((10#${day}))
        last_day=0
        if [[ $i_day -gt $duration ]]; then
            last_day=$(( $i_day - $duration ))
            #echo "The last date before Effective-duration: $month-$last_day"
            calc_last_date_rst="$month-$last_day"
            calc_last_month=$month
            calc_last_day=$last_day
        elif [[ $i_day -le $duration ]]; then
            previous_month_days=0
            case $(($i_month-1)) in
                1|3|5|7|8|10|12)
                    previous_month_days=31
                ;;
                4|6|9|11)
                    previous_month_days=30
                ;;
                2)
                    if [[ i_year%4 -eq 0 ]]; then
                        previous_month_days=29
                    elif [[ i_year%4 -ne 0 ]]; then
                        previous_month_days=28
                    fi
                ;;
            esac
            previous_month=$(($i_month-1))
            #echo $previous_month
            #echo $previous_month_days
            last_day=$(( $previous_month_days-($duration - $i_day) ))
            #echo "The last date before Effective-duration: $previous_month-$last_day"
            calc_last_date_rst="$previous_month-$last_day"
            calc_last_month=$previous_month
            calc_last_day=$last_day
        fi
    fi
}

##---------------------------------------------------------------------------------

today=`date | awk '{print $3}'`; int_today=$((10#${today}))
this_month=`date +"%m"`; int_this_month=$((10#${this_month}))
func_calc_last_date $db_bak_lifecycle_day
lifecycle_last_day=$calc_last_day
lifecycle_last_month=$calc_last_month
echo "[`date +"%Y-%m-%d %H:%M:%S"`] history full-backup lifecycle last date: $calc_last_date_rst"
echo "[`date +"%Y-%m-%d %H:%M:%S"`] history full-backup lifecycle last date: $calc_last_date_rst" >> $script_exec_log

idx=1; del_cnt=0
for item in ${db_bak_time_day[*]}; do
    history_day=$((10#${item})); history_month=${db_bak_time_month[$idx]}
    #echo "$history_month-$history_day, $int_this_month--$int_today, $lifecycle_last_month---$lifecycle_last_day"
    func_item_in_array "${db_bak_reserved_in_target_dir[*]}" "${db_bak[$idx]}"
    item_is_reserved=$?
    #echo "${db_bak[$idx]} reserved: $item_is_reserved"
    if [[ $history_day -lt $lifecycle_last_day ]] && [[ $history_day -lt $int_today ]] && [[ $item_is_reserved -eq 0 ]]; then
        echo "[`date +"%Y-%m-%d %H:%M:%S"`] -x-> ${db_bak[$idx]} should be deleted."
	echo "[`date +"%Y-%m-%d %H:%M:%S"`] -x-> ${db_bak[$idx]} should be deleted." >> $script_exec_log
	((del_cnt++))
    fi
    if [[ $history_day -gt $lifecycle_last_day ]] && [[ $history_month -lt $int_this_month ]] && [[ $item_is_reserved -eq 0 ]]; then
        echo "[`date +"%Y-%m-%d %H:%M:%S"`] -x-> ${db_bak[$idx]} should be deleted."
	echo "[`date +"%Y-%m-%d %H:%M:%S"`] -x-> ${db_bak[$idx]} should be deleted." >> $script_exec_log
	((del_cnt++))
    fi
    ((idx++))
done

echo "[`date +"%Y-%m-%d %H:%M:%S"`] total $del_cnt history full-backup deleted."
echo "[`date +"%Y-%m-%d %H:%M:%S"`] total $del_cnt history full-backup deleted." >> $script_exec_log
echo " " >> $script_exec_log
